package com.jd.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.entity.MessageEntity;
import com.jd.entity.OrderEntity;
import com.jd.entity.RulesEntity;
import com.jd.service.DetailedService;
import com.jd.service.MessageService;
import com.jd.service.OrderService;
import com.jd.service.RulesService;
import com.jd.service.UserService;
import com.jd.util.IdUtil;
import com.jd.util.OrderUtil;


@Controller
@RequestMapping("/orderController")
public class OrderController {
	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private RulesService rulesService;
	@Autowired
	private DetailedService detailedService;
	@Autowired
	private MessageService messageService;
	
	@RequestMapping("/withDrawal")
	@ResponseBody
	public String withDrawal(HttpServletRequest request){
		
		
		String userId = request.getSession().getAttribute("userId").toString();
		//用户钱包map
		HashMap<String,Object> map = userService.getUser(userId);
		if(map.get("state").toString().equals("1")){
			//提现金额
			Double price = Double.valueOf(request.getParameter("price"));
			String flag =request.getParameter("flag");
			String zfb_name =request.getParameter("zfb_name");
			String zfb_number =request.getParameter("zfb_number");
			HashMap<String, Object> user = userService.getUser(userId);
			//修改支付宝信息
			HashMap<String, Object> ZFBMap = new HashMap<String, Object>();
			ZFBMap.put("zfb_name", zfb_name);
			ZFBMap.put("zfb_number", zfb_number);
			ZFBMap.put("userId", userId);
			userService.updateZFB(ZFBMap);
			
			
			RulesEntity rules=rulesService.getRules();
			String maximumWithdrawal = rules.getMaximumWithdrawal();
			String minimumWithdrawal = rules.getMinimumWithdrawal();
			//手续费比例
			Double withdrawal = rules.getWithdrawal()*0.01;
			//手续费
			Double charge=price*withdrawal;
			//到账金额
			Double account=price-charge;
			//订单对象
			OrderEntity orderEntity = new OrderEntity();
			orderEntity.setCreateBy(userId);
			orderEntity.setId(IdUtil.getIdByUUId());
			orderEntity.setUserId(userId);
			orderEntity.setTitle("提现");
			orderEntity.setPrice(BigDecimal.valueOf(account));
			orderEntity.setOrderNumber(OrderUtil.getOrderIdByUUId());
			orderEntity.setOrderStatue("0");
			orderEntity.setCreateTime(new Date());
			orderEntity.setOrderTime(new Date());
			orderEntity.setDelflag("0");
			orderEntity.setRemark1(String.valueOf(charge));
			//flag=1  推广员钱包
			if(flag.equals("1")){
				//推广员余额
				Double balance =Double.valueOf(String.valueOf(map.get("balance")));
				if(price>balance){
					return "不凡币不足";
				}else if(Double.valueOf(minimumWithdrawal)>price){
					return "提现不凡币不得低于"+minimumWithdrawal+"不凡币";
				}else if(Double.valueOf(maximumWithdrawal)<price){
					return "提现不凡币不得高于"+maximumWithdrawal+"不凡币";
				}else{
					Double appreciating = Double.valueOf(String.valueOf(user.get("appreciating")));
					//插入订单表
					orderEntity.setType("2");
					orderService.insertWithDrawal(orderEntity);
					//提现剩余金额
					balance=balance-price;
					HashMap<String, Object> hashMap = new HashMap<String,Object>();
					hashMap.put("userId", userId);
					hashMap.put("balance", balance);
					hashMap.put("appreciating", price+appreciating);
					userService.updateBalance(hashMap);
					MessageEntity messageEntity = new MessageEntity();
					String mid=IdUtil.getIdByUUId();
					messageEntity.setId(mid);
					messageEntity.setUserId(userId);
					messageEntity.setTitle("申请提现已受理");
					messageEntity.setContent("已收到您的提现申请，提现"+price+"不凡币，将于48小时内转入您的支付宝账户，请注意查收。");
					messageEntity.setCreateBy(userId);
					messageEntity.setCreateTime(new Date());
					messageEntity.setRemark1("1");
					messageEntity.setDelflag("0");
					messageService.save(messageEntity);
					return "提交成功";
				}
			}else{
				//领赏余额
				Double balance =Double.valueOf(String.valueOf(map.get("mBalance")));
				if(price>balance){
					return "不凡币不足";
				}else if(Double.valueOf(minimumWithdrawal)>price){
					return "提现不凡币不得低于"+minimumWithdrawal+"不凡币";
				}else if(Double.valueOf(maximumWithdrawal)<price){
					return "提现不凡币不得高于"+maximumWithdrawal+"不凡币";
				}else{
					Double mappreciating = Double.valueOf(String.valueOf(user.get("mappreciating")));
					orderEntity.setType("1");
					orderService.insertWithDrawal(orderEntity);
					//提现剩余金额
					balance=balance-price;
					HashMap<String, Object> hashMap = new HashMap<String,Object>();
					hashMap.put("userId", userId);
					hashMap.put("balance", balance);
					hashMap.put("appreciating", price+mappreciating);
					userService.updateMBalance(hashMap);
					MessageEntity messageEntity = new MessageEntity();
					String mid=IdUtil.getIdByUUId();
					messageEntity.setId(mid);
					messageEntity.setUserId(userId);
					messageEntity.setTitle("申请领赏已受理");
					messageEntity.setContent("已收到您的领赏申请，领赏"+price+"不凡币，将于48小时内转入您的支付宝账户，请注意查收。");
					messageEntity.setCreateBy(userId);
					messageEntity.setCreateTime(new Date());
					messageEntity.setRemark1("1");
					messageEntity.setDelflag("0");
					messageService.save(messageEntity);
					return "提交成功";
				}
			}
		}else{
			return "请完成实名认证";
		}
	}

	@RequestMapping("/orderList")
	@ResponseBody
	public List<OrderEntity> orderList(HttpServletRequest request) throws JsonProcessingException {
		//String userId = request.getSession().getAttribute("userId").toString();
		List<OrderEntity> orderListByUserId = orderService.getOrderListByUserId("17f88611f5a24e89beee20780c30f1aa");
		return orderListByUserId;
	}
}
